package com.minimalist.lindi.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.entity.lindi.enums.LindiEnterpriseAccountEnum;
import com.minimalist.common.entity.lindi.po.LindiEnterpriseAccount;
import com.minimalist.common.entity.lindi.po.LindiEnterpriseRegistration;
import com.minimalist.common.entity.lindi.vo.enterpriseaccount.LindiEnterpriseAccountVO;
import com.minimalist.common.utils.GenerateUtil;
import com.minimalist.common.utils.Sm4Util;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiEnterpriseAccountMapper;
import com.minimalist.lindi.mapper.LindiEnterpriseRegistrationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 11987
 */
@Component
public class LindiEnterpriseManager{
    @Autowired
    private LindiEnterpriseRegistrationMapper lindiEnterpriseRegistrationMapper;

    @Autowired
    private LindiEnterpriseAccountMapper lindiEnterpriseAccountMapper;

    /**
     * 检查企业名是否已被注册
     *
     * @param enterpriseName 企业名
     * @return true：已被注册；false：未被注册
     */
    public Boolean checkEnterpriseNameRegistered(String enterpriseName, Long enterpriseAccountId){
        return lindiEnterpriseAccountMapper.selectEnterpriseAccountByEnterpriseName(enterpriseName, enterpriseAccountId) != null;
    }

    /**
     * 检查企业手机号是否已被注册
     *
     * @param enterpriseMobile 企业手机号
     * @return true：已被注册；false：未被注册
     */
    public Boolean checkEnterpriseMobileRegistered(String enterpriseMobile, Long enterpriseAccountId){
        return lindiEnterpriseAccountMapper.selectEnterpriseAccountByEnterpriseMobile(enterpriseMobile, enterpriseAccountId) != null;
    }

    public void createEnterpriseAccount(LindiEnterpriseRegistration lindiEnterpriseRegistration){
        Assert.isFalse(checkEnterpriseNameRegistered(
                        lindiEnterpriseRegistration.getEnterpriseName(), null)
                , () -> new BusinessException(LindiEnterpriseAccountEnum
                        .ErrorMsgEnum.ENTERPRISE_NAME_REGISTERED.getDesc()));
        Assert.isFalse(checkEnterpriseMobileRegistered(
                        lindiEnterpriseRegistration.getEnterpriseMobile(), null)
                , () -> new BusinessException(LindiEnterpriseAccountEnum
                        .ErrorMsgEnum.ENTERPRISE_MOBILE_REGISTERED.getDesc()));
        LindiEnterpriseAccount enterpriseAccount = new LindiEnterpriseAccount();
        enterpriseAccount.setEnterpriseAccountId(UnqIdUtil.uniqueId());
        enterpriseAccount.setStatus(LindiEnterpriseAccountEnum.StatusEnum
                .NORMAL.getCode());
        enterpriseAccount.setEnterprisePassword(Sm4Util.encrypt(
                GenerateUtil.generateRandomPassword(12)));
        BeanUtil.copyProperties(lindiEnterpriseRegistration, enterpriseAccount);
        lindiEnterpriseAccountMapper.insert(enterpriseAccount);
    }

    public LindiEnterpriseAccountVO getLindiEnterpriseAccountByEnterpriseAccountId(
            Long enterpriseAccountId){
        LindiEnterpriseAccount lindiEnterpriseAccount = lindiEnterpriseAccountMapper
                .selectEnterpriseAccountByEnterpriseAccountId(
                        enterpriseAccountId);
        return BeanUtil.copyProperties(lindiEnterpriseAccount, LindiEnterpriseAccountVO.class);
    }
}
